package org.carabiner.infinitest.swingui;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;

import org.carabiner.infinitest.TestStatusListener;

/**
 * 
 * @author <a href="mailto:benrady@gmail.com"Ben Rady</a>
 */
class TreeModelExpansionListener
{
  public static void watchTree(final JTree tree)
  {
    TreeModelListener listener = (TreeModelListener) Proxy.newProxyInstance(tree.getClass().getClassLoader(), new Class[] { TreeModelListener.class }, new InvocationHandler()
    {
      public Object invoke(Object proxy, final Method method, final Object[] args) throws Throwable
      {
        TreeModelEvent e = (TreeModelEvent) args[0];
        tree.expandPath(e.getTreePath());
        return null; 
      }
    });
    tree.getModel().addTreeModelListener(listener);
  }
}
